package samplesowl;
import java.io.File;

import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassExpression;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLIndividual;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;

public class ClassManipulationSample {

	public static void main(String[] args) {
		ClassManipulationSample cms = new ClassManipulationSample();
		try {
			long inicio = System.currentTimeMillis();
			cms.manipulationClass();;
			long fim = System.currentTimeMillis();
			double tempo = (fim - inicio) / 1000.0;
			System.out.println("TEMPO: " + tempo);
		} catch (OWLOntologyCreationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void manipulationClass() throws OWLOntologyCreationException {
		// String DOCUMENT_IRI = "http://www.w3.org/2002/07/owl";
		String DOCUMENT_IRI = "src/ontologies/wine.owl";
		File file = new File(DOCUMENT_IRI);
		// Create our ontology manager in the usual way.
		OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
		// Load a copy of the people+pets ontology. We'll load the ontology from
		// the web (it's acutally located in the TONES ontology repository).
		IRI docIRI = IRI.create(file);
		// We load the ontology from a document - our IRI points to it directly
		OWLOntology ontology = manager.loadOntologyFromOntologyDocument(docIRI);

		// Class<? extends OWLOntology> classe = ontology.getClass();
		// System.out.println(classe.getName());
		int count = 0;
		for (OWLClass cls : ontology.getClassesInSignature()) {
			System.out.println("Principal Class: " + cls);
			for (OWLClassExpression subclass : cls.getSubClasses(ontology)) {
				if (!subclass.equals(cls)) {
					System.out.println("      Relation: " + subclass);
				}
			}

			for (OWLClassExpression equivalent : cls
					.getEquivalentClasses(ontology)) {
				for (OWLClassExpression expression : equivalent.asConjunctSet()) {
					for (OWLObjectProperty property : expression.getObjectPropertiesInSignature()){
						System.out.println("      property: " + property);
					}
					
					for (OWLNamedIndividual signature : expression.getIndividualsInSignature()){
						System.out.println("      signature: " + signature);
					}
					
					for (OWLClass classes : expression.getClassesInSignature()){
						System.out.println("      classes: " + classes);
					}
					System.out.println("      --------------------------------------------");
					
				}
			}

			System.out.println("_____________________________________________________________________");
			count++;
		}
		System.out.println(count);
	}
	
	
	

	public void owlPrimer() throws OWLOntologyCreationException,
			OWLOntologyStorageException {
		// The OWLOntologyManager is at the heart of the OWL API, we can create
		// an instance of this using the OWLManager class, which will set up
		// commonly used options (such as which parsers are registered etc.
		// etc.)
		String DOCUMENT_IRI = "src/ontologies/generations.owl";
		File file = new File(DOCUMENT_IRI);
		OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
		// We want to create an ontology that corresponds to the ontology used
		// in the OWL Primer. Every ontology has a URI that uniquely identifies
		// the ontology. The URI is essentially a name for the ontology. Note
		// that the URI doesn't necessarily point to a location on the web - in
		// this example, we won't publish the ontology at the URL corresponding
		// to the ontology URI below.
		IRI ontologyIRI = IRI.create(file);
		// Now that we have a URI for out ontology, we can create the actual
		// ontology. Note that the create ontology method throws an
		// OWLOntologyCreationException if there was a problem creating the
		// ontology.
		OWLOntology ont = manager.createOntology(ontologyIRI);
		// We can use the manager to get a reference to an OWLDataFactory. The
		// data factory provides a point for creating OWL API objects such as
		// classes, properties and individuals.
		OWLDataFactory factory = manager.getOWLDataFactory();
		// We first need to create some references to individuals. All of our
		// individual must have URIs. A common convention is to take the URI of
		// an ontology, append a # and then a local name. For example we can
		// create the individual 'John', using the ontology URI and appending
		// #John. Note however, that there is no reuqirement that a URI of a
		// class, property or individual that is used in an ontology have a
		// correspondance with the URI of the ontology.
		OWLIndividual grandfather = factory.getOWLNamedIndividual(IRI
				.create(ontologyIRI + "#GrandFather"));
		OWLIndividual grandmother = factory.getOWLNamedIndividual(IRI
				.create(ontologyIRI + "#GrandMother"));
//		OWLIndividual mary = factory.getOWLNamedIndividual(IRI
//				.create(ontologyIRI + "#Mary"));
//		OWLIndividual susan = factory.getOWLNamedIndividual(IRI
//				.create(ontologyIRI + "#Susan"));
//		OWLIndividual bill = factory.getOWLNamedIndividual(IRI
//				.create(ontologyIRI + "#Bill"));
		// The ontologies that we created aren't contained in any ontology at
		// the moment. Individuals (or classes or properties) can't directly be
		// added to an ontology, they have to be used in axioms, and then the
		// axioms are added to an ontology. We now want to add some facts to the
		// ontology. These facts are otherwise known as property assertions. In
		// our case, we want to say that John has a wife Mary. To do this we
		// need to have a reference to the hasWife object property (object
		// properties link an individual to an individual, and data properties
		// link and individual to a constant - here, we need an object property
		// because John and Mary are individuals).
		OWLObjectProperty hasChild = factory.getOWLObjectProperty(IRI
				.create(ontologyIRI + "#hasChild"));
		// Now we need to create the assertion that John hasWife Mary. To do
		// this we need an axiom, in this case an object property assertion
		// axiom. This can be thought of as a "triple" that has a subject, john,
		// a predicate, hasWife and an object Mary
		OWLObjectPropertyAssertionAxiom axiom1 = factory
				.getOWLObjectPropertyAssertionAxiom(hasChild, grandfather, grandmother);
		
		System.out.println("aqui: " + hasChild);
	}

}
